package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.IdentityUniqueCheckDTO;
import com.engine4cloud.tdf.supervision.dto.create.JzdxCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxUpdateDTO;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 矫正对象-基本信息-web
 *
 * @author wenjie
 * @date 2024-03-14 11:20:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/corrector")
@Tag(description = "CorrectorWeb", name = "矫正对象-基本信息-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class JzdxWebController {

    private final JzdxService jzdxService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  矫正对象基本信息采集表
     * @return R<PageInfo < JzdxCorrectorResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<JzdxResultDTO>> getCorrectorPage(@ParameterObject Params page, @Valid @ParameterObject JzdxQueryDTO dto) {
        return R.ok(jzdxService.pageList(page, dto));
    }


    /**
     * 通过id查询矫正对象基本信息采集表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<JzdxResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(jzdxService.getById(id));
    }

    /**
     * 矫正对象基本信息-新增
     *
     * @param dto 矫正对象基本信息-新增
     * @return R
     */
    @Operation(summary = "新增矫正对象基本信息", description = "矫正对象基本信息-新增")
    @SysLog("新增矫正对象基本信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('corrector_add')")
    public R<String> create(@Valid @RequestBody JzdxCreateDTO dto) {
        return R.ok(jzdxService.create(dto));
    }

    /**
     * 矫正对象基本信息-编辑
     *
     * @param dto 矫正对象基本信息-编辑
     * @return R
     */
    @Operation(summary = "矫正对象基本信息-编辑", description = "矫正对象基本信息-编辑")
    @SysLog("修改矫正对象基本信息采集表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('corrector_edit')")
    public R<Boolean> updateById(@Valid @RequestBody JzdxUpdateDTO dto) {
        return R.ok(jzdxService.update(dto));
    }

    /**
     * 矫正对象删除
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "矫正对象删除", description = "矫正对象删除")
    @SysLog("通过id删除矫正对象基本信息采集表")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('corrector_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(jzdxService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 证件号是否唯一
     *
     * @param dto dto
     * @return 返回 true-唯一；false-不唯一
     */
    @Operation(summary = "证件号是否唯一", description = "证件号是否唯一")
    @PostMapping("/check")
    public R<Boolean> checkUnique(@Valid @RequestBody IdentityUniqueCheckDTO dto) {
        return R.ok(jzdxService.checkUniqueByIdentity(dto));
    }

    /**
     * 信息缺失度
     *
     * @param code 社区矫正对象编号
     * @return 返回 信息缺失项数量
     */
    @Operation(summary = "信息缺失度", description = "信息缺失度：等于0-完整；大于0-信息缺失")
    @GetMapping("/absence/{code}")
    public R<Integer> absence(@Schema(description = "矫正对象编号") @PathVariable("code") String code) {
        return R.ok(jzdxService.absence(code));
    }

}